package li.kaeppe.travel.tracker.web.backing;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import li.kaeppe.travel.tracker.domain.Trip;
import li.kaeppe.travel.tracker.service.TripService;

public class MapViewBacking {

	/* **************************************************** */
	/*                      CONSTANTS                       */
	/* **************************************************** */

	private static final Log log = LogFactory.getLog(MapViewBacking.class);
	
	/* **************************************************** */
	/*                     MEMBER FIELDS                    */
	/* **************************************************** */
	
	public String key;
	
	private TripService tripService;
	
	private Trip currentTrip = null;
	
	/* **************************************************** */
	/*                      CONSTRUCTOR                     */
	/* **************************************************** */

	/* **************************************************** */
	/*                    PUBLIC METHODS                    */
	/* **************************************************** */
	
	public Trip getCurrentTrip() {
		
		if(this.currentTrip == null) { 
			try {
			this.currentTrip = this.getTripService().getTripByKey(this.getKey(), "");
			} catch (Throwable th) {
				this.currentTrip = new Trip(null, 
						"No trip found",
						"The requested trip wasn't found!",
						new Date(),
						new Date());
				log.warn("Can't load trip with id [" + this.getKey() + "]", th);
			}
		}
		return this.currentTrip;
	} 
	
	/* **************************************************** */
	/*                  PROTECTED METHODS                   */
	/* **************************************************** */

	/* **************************************************** */
	/*                   PRIVATE METHODS                    */
	/* **************************************************** */

	/* **************************************************** */
	/*                  GETTERS AND SETTERS                 */
	/* **************************************************** */
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public TripService getTripService() {
		return tripService;
	}

	public void setTripService(TripService tripService) {
		this.tripService = tripService;
	}
}
